package com.gofaraway.start.config;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author ChenPeng
 * @Date 2024/1/18 17:20
 */
@Configuration
@MapperScan(basePackages = {"com.gofaraway.dao.mysql.sky.mapper", "com.gofaraway.dao.mysql.trunk.mapper"}
        , sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSourceConfig {


    @Autowired
    private PaginationInterceptor paginationInterceptor;

    /**
     * 配置mysql数据库
     *
     * @return
     */
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DataSource tmsDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 创建 mysql数据库的 SessionFactory
     *
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory tmsSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        Interceptor[] plugins = {paginationInterceptor};
        bean.setPlugins(plugins);
        //bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/mysql/*Mapper.xml"));
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/mysql/*/*Mapper.xml"));
        //bean.setGlobalConfig(globalConfig);
        return bean.getObject();
    }

    /**
     * 创建 mysql数据库的 事务
     *
     * @param dataSource
     * @return
     */
    @Bean(name = "transactionManager")
    @Primary
    public DataSourceTransactionManager tmsTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionTemplate")
    @Primary
    public SqlSessionTemplate tmsSqlSessionTemplate(
            @Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}
